#include "FS.h"
#include <LittleFS.h>


String writeFile(const char * path, const char * message) {
  String ret = "";
  Serial.printf("Writing file: %s\n", path);

  if (!LittleFS.begin()) {
    ret = ("LittleFS mount failed");
    return ret;
  }

  File file = LittleFS.open(path, "w+");
  if (!file) {
    ret = ("Failed to open file for writing");
    return ret;
  }
  if (file.print(message)) {
    ret = ("File written");
  } else {
    ret = ("Write failed");
  }
  delay(2000); // Make sure the CREATE and LASTWRITE times are different
  file.close();
  
  LittleFS.end();
  
  return ret;
}

String appendFile(const char * path, const char * message) {
  String ret = "";
  Serial.printf("Appending to file: %s\n", path);

  if (!LittleFS.begin()) {
    ret = ("LittleFS mount failed");
    return ret;
  }

  File file = LittleFS.open(path, "a");
  if (!file) {
    ret = ("Failed to open file for appending");
    return ret;
  }
  if (file.print(message)) {
    ret = ("Message appended");
  } else {
    ret = ("Append failed");
  }
  file.close();
  
  LittleFS.end();
  
  return ret;
}

String readFile(const char * path) {
  String ret = "";
  Serial.printf("Reading file: %s\n", path);

  if (!LittleFS.begin()) {
    ret = ("LittleFS mount failed");
    return ret;
  }

  File file = LittleFS.open(path, "r");
  if (!file) {
    ret = ("Failed to open file for reading");
    return ret;
  }

  Serial.print("Read from file: ");
  ret = file.readString();
  file.close();
  
  LittleFS.end();
  
  return ret;
}



String deleteFile(const char * path) {
  String ret = "";
  Serial.printf("Deleting file: %s\n", path);

  if (!LittleFS.begin()) {
    ret = ("LittleFS mount failed");
    return ret; 
  }

  if (LittleFS.remove(path)) {
    ret = ("File deleted");
  } else {
    ret = ("Delete failed");
  }
  LittleFS.end();
  return ret;
}




String listDir(const char * dirname) {
  Serial.printf("Listing directory: %s\n", dirname);  

  if (!LittleFS.begin()) {
    Serial.println ("LittleFS mount failed"); 
  }

  Dir root = LittleFS.openDir(dirname);

  String buf = "";

  while (root.next()) {
    File file = root.openFile("r");
    Serial.print("  FILE: ");
    Serial.print(root.fileName());
    Serial.print("  SIZE: ");
    Serial.print(file.size());
    time_t cr = file.getCreationTime();
    time_t lw = file.getLastWrite();

    buf += root.fileName() + ", " + String(file.size()) + "\n";    
      
    file.close();
    struct tm * tmstruct = localtime(&cr);
    Serial.printf("    CREATION: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
    tmstruct = localtime(&lw);
    Serial.printf("  LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
  }

  return buf; 
}